<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function pr($msg)
{
    $data = [
        'type'    => 'pr',
        'info'    => '',
        'content' => $msg
    ];
    db('log', [], false)->insert($data);
}

function prd()
{
    $info = func_get_args();
    echo var_dump($info) . "\n";
}

function prw($msg)
{
    echo $msg . "\n";
}

function dump()
{
    $info = func_get_args();

    $debug = debug_backtrace();
    $line  = 'line:[' . $debug[0]['line'] . '] in >>>[' . $debug[0]['file'] . ']';
    $data  = [
        'type'     => 'dump',
        'info'     => $line,
        'content'  => print_r($info, true),
        'class'    => $debug[1]['class'],
        'function' => $debug[1]['function']
    ];
    db('log', [], false)->insert($data);


    $suiji = mt_rand(0, 10);
    if ($suiji == 5) {
        $ai   = db('log', [], false)->insertGetId($data);
        $cunt = db('log', [], false)->count();
        if ($cunt > 1000) {
            $where = [
                'id' => ['<', $ai - 800]
            ];
            $re    = db('log', [], false)->where($where)->delete();
            prd($ai, $cunt, $re);
        }
    }
}

function db($name = '', $config = [], $force = false)
{
    return \think\Db::connect($config, $force)->name($name);
}

/**
 * 对象数组转为普通数组
 *
 * AJAX提交到后台的JSON字串经decode解码后为一个对象数组，
 * 为此必须转为普通数组后才能进行后续处理，
 * 此函数支持多维数组处理。
 *
 * @param array
 * @return array
 */
function objarray_to_array($obj)
{
    $ret = array();
    foreach ($obj as $key => $value) {
        if (gettype($value) == "array" || gettype($value) == "object") {
            $ret[$key] = objarray_to_array($value);
        } else {
            $ret[$key] = $value;
        }
    }
    return $ret;
}

/**
 *
 * @param type $username
 * @param type $password
 * @param type $type
 * @return string
 */
function md6($username, $password, $type = 's64')
{
    $p2 = $u1 = md5($username);
    $u2 = $p1 = md5($password);
    $pu = md5($username . $password);
    foreach (str_split($pu) as $value) {

        //dump($value);
        if ($value > 3) {
            $p2 .= substr($u1, 1, 1);
            $u2 .= substr($p1, 1, 1);
        } else {
            $p2 .= substr($p1, 1, 1);
            $u2 .= substr($u1, 1, 1);
        }
        $p1 = substr($p1, 1);
        $u1 = substr($u1, 1);
    }

    switch ($type) {
        case 'b64':
            $md6 = strtoupper(md5($u2) . md5($p2));
            break;
        case 's64':
            $md6 = md5($u2) . md5($p2);
            break;
    }

    return $md6;
}

/**
 * Session管理
 * @param string|array  $name session名称，如果为数组表示进行session设置
 * @param mixed         $value session值
 * @param string        $prefix 前缀
 * @return mixed
 */
function session($name, $value = '', $prefix = null)
{
    if (is_null($name)) {
        // 清除
        GatewayWorker\Lib\Session::clear($value);
    } elseif ('' === $value) {
        // 判断或获取
        return 0 === strpos($name, '?') ? GatewayWorker\Lib\Session::has(substr($name, 1), $prefix) : GatewayWorker\Lib\Session::get($name, $prefix);
    } elseif (is_null($value)) {
        // 删除
        return GatewayWorker\Lib\Session::delete($name, $prefix);
    } else {
        // 设置
        return GatewayWorker\Lib\Session::set($name, $value, $prefix);
    }
}
